home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / source / snip9503 / modemio.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-14  |  5.3 KB  |  243 lines

  1. /*
  2. ** This source is released to the Public
  3. ** domain on December 16, 1992.
  4. **
  5. ** Curtis Paris
  6. ** Internet: cparis@comtch.spk.wa.usa
  7. **
  8. ** modified 12-Mar-94 by Bob Stout - Added portable.h from SNIPPETS
  9. */
  10.  
  11.  
  12. #include <dos.h>
  13. #include <ctype.h>
  14. #include "portable.h"
  15.  
  16. #define MODEMIO_INIT
  17. #include "modemio.h"
  18.  
  19. void (_interrupt _far *old_modem_isr)(void);
  20.  
  21. /*********************************************************************/
  22. void _far interrupt modem_isr(void)
  23. {
  24.   unsigned char c;
  25.  
  26.   INT_ON();
  27.  
  28.   if (modem_buffer_count<1024) {
  29.     c=IN_PORT(modem_base);
  30.     if (((c==XON) || (c==XOFF)) && (modem_xon_xoff)) {
  31.       switch(c) {
  32.         case XON :modem_pause=0; break;
  33.         case XOFF:modem_pause=1; break;
  34.       }
  35.     } else {
  36.       modem_pause=0;
  37.       modem_buffer[modem_buffer_head++]=c;
  38.       if (modem_buffer_head>=MAX_BUFFER) modem_buffer_head=0;
  39.       modem_buffer_count++;
  40.     }
  41.     modem_overflow=0;
  42.   } else {
  43.     modem_overflow=1;
  44.   }
  45.   INT_OFF();
  46.   OUT_PORT(0x20,0x20);
  47. }
  48.  
  49. int com_carrier(void)
  50. {
  51.   int x;
  52.  
  53.   if (!modem_open) return(0);
  54.   if ((IN_PORT(modem_base+6) & 0x80)==128) return(1);
  55.  
  56.   for (x=0; x<500; x++) {
  57.     if ((IN_PORT(modem_base+6) & 0x80)==128) return(1);
  58.   }
  59.   return(0);
  60. }
  61.  
  62. int com_ch_ready(void)
  63. {
  64.   if (!modem_open) return(0);
  65.   if (modem_buffer_count!=0) return(1);
  66.   return(0);
  67. }
  68.  
  69. unsigned char com_read_ch(void)
  70. /* This will return 0 is there is no character waiting.  Please check
  71.    the port with com_ch_ready(); first so that if they DID send a 0x00
  72.    that you will know it's a true 0, not a no character return!
  73. */
  74. {
  75.   unsigned char ch;
  76.  
  77.   if (!modem_open) return(0);
  78.   if (!com_ch_ready()) return(0);
  79.   ch=modem_buffer[modem_buffer_tail];
  80.   modem_buffer[modem_buffer_tail]=0;
  81.   modem_buffer_count--;
  82.   if (++modem_buffer_tail>=MAX_BUFFER) modem_buffer_tail=0;
  83.   return(ch);
  84. }
  85.  
  86. void com_send_ch(unsigned char ch)
  87. {
  88.   if (!modem_open) return;
  89.   OUT_PORT(modem_base+4,0x0B);
  90.   if (modem_rts_cts) {
  91.     while((IN_PORT(modem_base+6) & 0x10)!=0x10) ; /* Wait for Clear to Send */
  92.   }
  93.   while((IN_PORT(modem_base+5) & 0x20)!=0x20) ;
  94.   if (modem_xon_xoff) {
  95.     while((modem_pause) && (com_carrier())) ;
  96.   }
  97.   OUT_PORT(modem_base,ch);
  98. }
  99.  
  100.  
  101. void com_parity(char p)
  102. {
  103.   int x, newb=0;
  104.  
  105.   if (!modem_open) return;
  106.   x=IN_PORT(modem_base+3);
  107.  
  108.   newb=(x>>6<<6)+(x<<5>>5); /* Get rid of old parity */
  109.  
  110.   switch(toupper(p)) {
  111.     case 'N':newb+=0x00; break; /* None  */
  112.     case 'O':newb+=0x08;break;  /* Odd   */
  113.     case 'E':newb+=0x18; break; /* Even  */
  114.     case 'M':newb+=0x28;break;  /* Mark  */
  115.     case 'S':newb+=0x38;break;  /* Space */
  116.   }
  117.  
  118.   OUT_PORT(modem_base+3, newb);
  119. }
  120.  
  121. void com_data_bits(unsigned char bits)
  122. {
  123.   int x, newb=0;
  124.  
  125.   if (!modem_open) return;
  126.   x=IN_PORT(modem_base+3);
  127.  
  128.     newb=(x>>2<<2); /* Get rid of the old Data Bits */
  129.  
  130.   switch(bits) {
  131.     case 5 :newb+=0x00; break;
  132.     case 6 :newb+=0x01; break;
  133.     case 7 :newb+=0x02; break;
  134.     default:newb+=0x03; break;
  135.   }
  136.  
  137.   OUT_PORT(modem_base+3,newb);
  138. }
  139.  
  140. void com_stop_bits(unsigned char bits)
  141. {
  142.   int x, newb=0;
  143.  
  144.   if (!modem_open) return;
  145.   x=IN_PORT(modem_base+3);
  146.  
  147.   newb=(x<<6>>6)+(x>>5<<5); /* Kill the old Stop Bits */
  148.  
  149.   if (bits==2) newb+=0x04; /* Only check for 2, assume 1 otherwise */
  150.  
  151.   OUT_PORT(modem_base+3,newb);
  152. }
  153.  
  154. void com_speed(long speed)
  155. {
  156.   int x;
  157.     char l, m;
  158.     int d;
  159.  
  160.   if (!modem_open) return;
  161.  
  162.   x=IN_PORT(modem_base+3); /* Read In Old Stats */
  163.  
  164.   if ((x & 0x80)!=0x80) OUT_PORT(modem_base+3,x+0x80); /* Set DLab On */
  165.  
  166.   d=(int)(115200L/speed);
  167.   l=d & 0xFF;
  168.   m=(d >> 8) & 0xFF;
  169.  
  170.   OUT_PORT(modem_base+0,l);
  171.   OUT_PORT(modem_base+1,m);
  172.  
  173.   OUT_PORT(modem_base+3,x); /* Restore the DLAB bit */
  174. }
  175.  
  176. int com_open(int comport, long speed, int data_bit, unsigned char parity,
  177.       unsigned char stop_bit)
  178. {
  179.   int x;
  180.  
  181.   INT_OFF();
  182.   if (modem_open) com_close();
  183.   modem_port=comport;
  184.  
  185.   switch(modem_port) {
  186.     case 1:modem_base=0x3F8; modem_irq=4; modem_vect=0x0C; break;
  187.     case 2:modem_base=0x2F8; modem_irq=3; modem_vect=0x0B; break;
  188.     case 3:modem_base=0x3E8; modem_irq=4; modem_vect=0x0B; break;
  189.     case 4:modem_base=0x2E8; modem_irq=3; modem_vect=0x0C; break;
  190.     case 5:break;
  191.     default:modem_base=0x3F8; modem_irq=4; modem_vect=0x0C; break;
  192.   }
  193.  
  194.   OUT_PORT(modem_base+1,0x00);
  195.   if (IN_PORT(modem_base+1)!=0) {
  196.     INT_ON();
  197.     return(0);
  198.   }
  199.  
  200.   /* Set up the Interupt Info */
  201.   old_modem_ier=IN_PORT(modem_base+1);
  202.   OUT_PORT(modem_base+1,0x01);
  203.  
  204.   old_modem_isr=(void (_interrupt _far *)(void))GETVECT(modem_vect);
  205.   SETVECT(modem_vect,modem_isr);
  206.  
  207.   if (modem_rts_cts) {
  208.     OUT_PORT(modem_base+4,0x0B);
  209.   } else {
  210.     OUT_PORT(modem_base+4,0x09);
  211.   }
  212.  
  213.   old_modem_imr=IN_PORT(I8088_IMR);
  214.   OUT_PORT(I8088_IMR,old_modem_imr & ((1 << modem_irq) ^ 0x00FF));
  215.  
  216.   for (x=1; x<=5; x++) IN_PORT(modem_base+x);
  217.  
  218.   modem_open=1;
  219.  
  220.   modem_buffer_count=0;
  221.   modem_buffer_head=0;
  222.   modem_buffer_tail=0;
  223.  
  224.   com_speed(speed);
  225.   com_data_bits((unsigned char)data_bit);
  226.   com_parity(parity);
  227.   com_stop_bits(stop_bit);
  228.   INT_ON();
  229.   return(1);
  230. }
  231.  
  232. void com_close(void)
  233. {
  234.   if (!modem_open) return;
  235.  
  236.   OUT_PORT(modem_base+1,old_modem_ier);
  237.   OUT_PORT(I8088_IMR, old_modem_imr);
  238.  
  239.   SETVECT(modem_vect, old_modem_isr);
  240.   OUT_PORT(0x20,0x20);
  241.   modem_open=0;
  242. }
  243.